package com.atwulidun.prototype03.deepcopy;

import java.io.*;

public class DeepCopy {
    public static void main(String[] args) {
        Sheep sheep = new Sheep("喜洋洋", 3, "白色");
        // 设置sheep中的引用类型成员变量
        sheep.setWolf(new Wolf("灰太狼",1));
        // 利用第一种方式拷贝sheep
        Sheep sheep1 = (Sheep) sheep.clone();
        Sheep sheep2 = (Sheep) sheep.clone();
        Sheep sheep3 = (Sheep) sheep.clone();
        // 输出克隆对象中引用成员变量的hashCode，发现都是不一样的，这就是深拷贝
        System.out.println("以下为深拷贝的第一种方式：");
        System.out.println(sheep.getWolf().hashCode());
        System.out.println(sheep1.getWolf().hashCode());
        System.out.println(sheep2.getWolf().hashCode());
        System.out.println(sheep3.getWolf().hashCode());
        // 利用第二种方式拷贝sheep
        Sheep sheep4 = (Sheep) sheep.deepCopy();
        Sheep sheep5 = (Sheep) sheep.deepCopy();
        Sheep sheep6 = (Sheep) sheep.deepCopy();
        // 输出克隆对象中引用成员变量的hashCode，发现都是不一样的，这就是深拷贝
        System.out.println("以下为深拷贝的第二种方式：");
        System.out.println(sheep.getWolf().hashCode());
        System.out.println(sheep4.getWolf().hashCode());
        System.out.println(sheep5.getWolf().hashCode());
        System.out.println(sheep6.getWolf().hashCode());
    }
}
// 深拷贝有两种实现方式：
// 方式一：被拷贝对象的类实现Cloneable接口，重写clone方法，对基本数据类型(包括字符串)以及引用数据类型分开做处理
// 方式二：被拷贝对象的类实现Serializable接口，利用序列化和反序列化实现深拷贝
class Sheep implements Cloneable,Serializable{
    private String name;
    private String color;
    private int age;
    // 增加一个引用数据类型来测试深拷贝
    private Wolf wolf;

    public Sheep(String name, int age, String color) {
        this.name = name;
        this.age = age;
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Wolf getWolf() {
        return wolf;
    }

    public void setWolf(Wolf wolf) {
        this.wolf = wolf;
    }

    @Override
    public String toString() {
        return "Sheep{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", color='" + color + '\'' +
                '}';
    }

    // 方式一
    @Override
    protected Object clone() {
        Sheep sheep = null;
        try {
            // 第一步：对基本数据类型和字符串类型成员变量进行拷贝
            sheep = (Sheep) super.clone();
            // 第二步：对引用数据类型成员变量要另外处理
            sheep.setWolf((Wolf) wolf.clone());
        } catch (CloneNotSupportedException e) {
            System.out.println(e.getMessage());;
        }
        return sheep;
    }

    // 方式二
    public Sheep deepCopy() {
        try (ByteArrayOutputStream bao = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(bao);
             ByteArrayInputStream bai = new ByteArrayInputStream(bao.toByteArray());
             ObjectInputStream ois = new ObjectInputStream(bai)) {
            // 第一步：对当前对象进行序列化
            oos.writeObject(this);
            // 第二步：对当前对象进行反序列化
            return (Sheep) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            return null;
        }
    }
}
